package com.fzu.express.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 * @author Administrator
 */
public class DateUtil {

    /**
     * 获取count天前的日期
     * @param count
     * @return
     */
    public static Date getAgoDate(Integer count) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - count);
        return calendar.getTime();
    }

    /**
     * 获取count天前到今日的日期列表(YYYY-MM-DD)
     * @param count
     * @return
     */
    public static List<String> getAgoDateList(Integer count) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - count);
        Date temp = calendar.getTime();

        List<String> dateList = new ArrayList<>(count + 1);
        dateList.add(simpleDateFormat.format(temp));
        while (count > 0) {
            temp = getNextDay(temp);
            dateList.add(simpleDateFormat.format(temp));
            count --;
        }

        return dateList;
    }

    /**
     * 获取当前的月份
     * @return
     */
    public static Integer getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }


    /**
     * 获取下一天的日期
     * @param date
     * @return
     */
    public static Date getNextDay(Date date) {
        //1天24小时，1小时60分钟，1分钟60秒，1秒1000毫秒
        long addTime = 24 * 60 * 60 * 1000;
        return new  Date(date.getTime() + addTime);
    }
}
